<?php

if(!defined('IN_FANWE'))
	exit('Access Denied');

class Upload
{
	var $file = array();
	var $dir = 'public';
	var $error_code = 0;
	var $max_size = 2048;

	function Upload()
	{

	}

	function init($file, $dir = 'public')
	{
		if(!is_array($file) || empty($file) || !$this->isUploadFile($file['tmp_name']) || trim($file['name']) == '' || $file['size'] == 0)
		{
			$this->file = array();
			$this->error_code = -1;
			return false;
		}
		else
		{
			$file['size'] = intval($file['size']);
			$file['name'] =  trim($file['name']);
			$file['thumb'] = '';
			$file['ext'] = $this->fileExt($file['name']);
			$file['name'] =  htmlspecialchars($file['name'], ENT_QUOTES);

			$file['is_image'] = $this->isImageExt($file['ext']);
			$file['file_dir'] = $this->getTargetDir($dir);
			$file['target'] = $file['file_dir'].'/'.md5(microtime(true)).'.'.$file['ext'];
			$file['local_target'] = FANWE_ROOT.$file['target'];
			$this->file = &$file;
			$this->error_code = 0;
			return true;
		}

	}

	function save()
	{
		if(empty($this->file) || empty($this->file['tmp_name']))
			$this->error_code = -101;
		elseif(!$this->file['is_image'] && $this->file['ext'] != 'swf')
			$this->error_code = -102;
		elseif(!$this->saveFile($this->file['tmp_name'], $this->file['local_target']))
			$this->error_code = -103;
		elseif(($this->file['is_image'] || $this->file['ext'] == 'swf') && (!$this->file['image_info'] = $this->getImageInfo($this->file['local_target'], true)))
		{
			$this->error_code = -104;
			@unlink($this->file['local_target']);
		}
		else
		{
			$this->error_code = 0;
			return true;
		}
		return false;
	}

	function error()
	{
		return $this->error_code;
	}

	function fileExt($file_name)
	{
		return addslashes(strtolower(substr(strrchr($file_name, '.'), 1, 10)));
	}

	function isImageExt($ext)
	{
		static $img_ext  = array('jpg', 'jpeg', 'png', 'bmp');
		return in_array($ext, $img_ext) ? 1 : 0;
	}

	function getImageInfo($target, $allow_swf = false)
	{
		$ext = Upload::fileExt($target);
		$is_image = Upload::isImageExt($ext);

		if(!$is_image && ($ext != 'swf' || !$allow_swf))
			return false;
		elseif(!is_readable($target))
			return false;
		elseif($image_info = @getimagesize($target))
		{
			list($width, $height, $type) = !empty($image_info) ? $image_info : array('', '', '');
			$size = $width * $height;
			if($size > 2097152 || $size < 16 )
				return false;
			elseif($ext == 'swf' && $type != 4 && $type != 13)
				return false;
			elseif($is_image && !in_array($type, array(1,2,3,6,13)))
				return false;
			return $image_info;
		}
		else
			return false;
	}

	function isUploadFile($source)
	{
		return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file(str_replace('\\\\', '\\', $source)));
	}

	function getTargetDir($dir)
	{
		$dir = 'public/upload/'.$dir.'/'.toDate(NULL,'Ym');
		makeDir(FANWE_ROOT.$dir);
		return $dir;
	}

	private function saveFile($source, $target)
	{
		if(!Upload::isUploadFile($source))
			$succeed = false;
		elseif(@copy($source, $target))
			$succeed = true;
		elseif(function_exists('move_uploaded_file') && @move_uploaded_file($source, $target))
			$succeed = true;
		elseif (@is_readable($source) && (@$fp_s = fopen($source, 'rb')) && (@$fp_t = fopen($target, 'wb')))
		{
			while (!feof($fp_s))
			{
				$s = @fread($fp_s, 1024 * 512);
				@fwrite($fp_t, $s);
			}
			fclose($fp_s);
			fclose($fp_t);
			$succeed = true;
		}

		if($succeed)
		{
			$this->error_code = 0;
			@chmod($target, 0644);
			@unlink($source);
		}
		else
		{
			$this->error_code = 0;
		}

		return $succeed;
	}
}
?>